﻿using RealtorsPortal.Business_Logic_Layer;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace RealtorsPortal.AdminCP
{
    public partial class EditAdvertisement : System.Web.UI.Page
    {
        AdvertisementsDAL advDalObj = new AdvertisementsDAL();
        AdvertisementsDAO advObj = new AdvertisementsDAO();
        CountriesDAL countriesDalObj = new CountriesDAL();
        RegionDAL regionDalObj = new RegionDAL();
        CitiesDAL citiesDalObj = new CitiesDAL();
        DistrictsDAL districtsDalObj = new DistrictsDAL();
        CategoriesDAL cateDalObj = new CategoriesDAL();
        ParkagesDAL parkDalObj = new ParkagesDAL();
        AdsImages adsImageObj = new AdsImages();
        AdsImagesDAL adsDal = new AdsImagesDAL();
        string[] strUrl = new string[0];
        int[] rowId = new int[0];

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null)
            {
                advObj.AdvId = Convert.ToInt32(Request.QueryString["ID"].ToString());
            }
            if(!IsPostBack){
                GetCountries();
                GetCategories();
                GetPackages();
                LoadAdvertisementDetails();
                ddlCountry.Items.Insert(0, "--Select Country--");
                ddlRegion.Items.Insert(0, "--Select Region--");
                ddlPackages.Items.Insert(0, "--Select Package--");
                ddlCategory.Items.Insert(0, "--Select Category--");
            }
            //Response.Write(advObj.AdvId);
        }
        #region LoadData
        protected void GetPackages() {
            DataTable dt = new DataTable();
            dt = parkDalObj.getPackagesData();
            ddlPackages.DataSource = dt;
            ddlPackages.DataValueField = "PackageId";
            ddlPackages.DataTextField = "PackageName";
            ddlPackages.DataBind();
        }

        protected void GetCategories() {
            DataTable dt = new DataTable();
            dt = cateDalObj.GetCategoriessData();
            ddlCategory.DataSource = dt;
            ddlCategory.DataValueField = "CategoryId";
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataBind();
        }

        protected void GetCountryToTextBox() {
            DataTable dt = new DataTable();
            dt = countriesDalObj.getCountriesData();
            txtCountry.Text = dt.Rows[0]["CountryName"].ToString();
        }

        protected void GetCountries()
        {
            DataTable dt = new DataTable();
            dt = countriesDalObj.getCountriesData();
            #region example
            //string tablestring = "";
            //dt is datatable object which holds DB results.
            //tablestring = tablestring + "<table width='100%'><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
            //string rowtable = "<tr><td>";
            //string coltable = "<td>";
            //for (int i = 0; i < dt.Rows.Count; )
            ////foreach (DataRow dr in dt.Rows)
            //{
            //    if (i < dt.Rows.Count)
            //    {
            //        tablestring = tablestring + rowtable + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[i]["CountryName"].ToString() + "</td>";
            //    }
            //    if (i + 1 < dt.Rows.Count)
            //    {
            //        tablestring = tablestring + coltable + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[i + 1]["CountryName"].ToString() + "</td>";
            //    }
            //    if (i + 2 < dt.Rows.Count)
            //    {
            //        tablestring = tablestring + coltable + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[i + 2]["CountryName"].ToString() + "</td></tr>";
            //    }
            //    i = i + 3;
            //}
            //tablestring = tablestring + "</table>";

            //dialog.InnerHtml = tablestring;

            //for (int i = 0; i < dt.Rows.Count;)
            //{
                
            //    HtmlGenericControl divRow = new HtmlGenericControl("div");

            //    divRow.ID = "divRow" + i;
            //    divRow.Style.Add(HtmlTextWriterStyle.Width, "99%");
            //    divRow.Style.Add(HtmlTextWriterStyle.Display, "block");
            //    divRow.Style.Add(HtmlTextWriterStyle.PaddingBottom, "5px");
            //    divRow.Style.Add(HtmlTextWriterStyle.BackgroundColor, "red");

            //    if (i < dt.Rows.Count)
            //    {
            //        HtmlGenericControl div = new HtmlGenericControl("div");
            //        HtmlGenericControl divCol = new HtmlGenericControl("div");
            //        divCol.ID = "divCol" + i;
                    
            //        divCol.Style.Add(HtmlTextWriterStyle.Width, "24%");
            //        divCol.Style.Add(HtmlTextWriterStyle.Display, "block");

            //        div.ID = "div" + i;
                    
            //        div.InnerHtml = dt.Rows[i]["CountryName"].ToString();
            //        divCol.Controls.Add(div);
            //        divRow.Controls.Add(divCol);
            //        dialog.Controls.Add(divRow);
                    
            //    }
            //    if (i + 1 < dt.Rows.Count)
            //    {
            //        HtmlGenericControl div = new HtmlGenericControl("div");
            //        HtmlGenericControl divCol = new HtmlGenericControl("div");

            //        divCol.ID = "divCol" + i+1;
            //        divCol.Style.Add(HtmlTextWriterStyle.Width, "24%");
            //        divCol.Style.Add(HtmlTextWriterStyle.Display, "block");

            //        div.ID = "div" + i+1;
            //        div.InnerHtml = dt.Rows[i+1]["CountryName"].ToString();
            //        divCol.Controls.Add(div);
            //        divRow.Controls.Add(divCol);
            //        dialog.Controls.Add(divRow);
            //    }
            //    if (i + 2 < dt.Rows.Count)
            //    {
            //        HtmlGenericControl div = new HtmlGenericControl("div");
            //        HtmlGenericControl divCol = new HtmlGenericControl("div");

            //        divCol.ID = "divCol" + i+2;
            //        divCol.Style.Add(HtmlTextWriterStyle.Width, "24%");
            //        divCol.Style.Add(HtmlTextWriterStyle.Display, "block");

            //        div.ID = "div" + i+2;

            //        div.InnerHtml = dt.Rows[i+2]["CountryName"].ToString();
            //        divCol.Controls.Add(div);
            //        divRow.Controls.Add(divCol);
            //        dialog.Controls.Add(divRow);
            //    }
            //    i = i + 3;
            //}
            #endregion

            ddlCountry.DataSource = dt;
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataValueField = "CountryId";
            ddlCountry.DataBind();
        }

        private DataTable GetCountryById(Country countryObj)
        {
            DataTable dt = new DataTable();
            dt = countriesDalObj.GetCountriesById(countryObj);
            return dt;
        }

        private DataTable GetRegionById(Region regionObj) {
            DataTable dt = new DataTable();
            dt = regionDalObj.GetRegionsByRegionId(regionObj);
            return dt;
        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            Country countryObj = new Country();
            countryObj.CountryId = Convert.ToInt16(ddlCountry.SelectedValue.ToString());
            dt = GetCountryById(countryObj);
            txtCountry.Text = dt.Rows[0]["CountryName"].ToString();
            Region regObj = new Region();
            regObj.CountryId = int.Parse(ddlCountry.SelectedValue);
            GetRegions(regObj);
            ddlRegion.Items.Insert(0, "--Select Region--");
        }

        protected void GetRegions(Region regObj)
        {
            DataTable dt = new DataTable();
            dt = regionDalObj.GetRegionsByCountryId(regObj);
            ddlRegion.DataSource = dt;
            ddlRegion.DataValueField = "RegionId";
            ddlRegion.DataTextField = "RegionName";
            ddlRegion.DataBind();
        }

        private void LoadAdvertisementDetails()
        {
            DataTable dt = new DataTable();
            dt = advDalObj.GetAdvertisementDetails(advObj);
            txtPackages.Text = dt.Rows[0]["PackageName"].ToString();
            txtCountry.Text = dt.Rows[0]["CountryName"].ToString();
            txtCategory.Text = dt.Rows[0]["CategoryName"].ToString();
            txtRegion.Text = dt.Rows[0]["RegionName"].ToString();
            txtCity.Text = dt.Rows[0]["CityName"].ToString();
            txtDistrict.Text = dt.Rows[0]["DistrictName"].ToString();
            txtTitle.Text = dt.Rows[0]["Title"].ToString();
            txtAddress.Text = dt.Rows[0]["Address"].ToString();
            txtPrice.Text = dt.Rows[0]["Price"].ToString();
            txtArea.Text = dt.Rows[0]["LandArea"].ToString();
            if (dt.Rows[0]["CategoryName"].ToString() == "Land")
            {
                
            }
            else {
                txtFloor.Text = dt.Rows[0]["Floor"].ToString();
                txtBuildYear.Text = dt.Rows[0]["BuiltYear"].ToString();
                txtBathRoom.Text = dt.Rows[0]["BathRoom"].ToString();
                txtBedRoom.Text = dt.Rows[0]["BedRoom"].ToString();
                txtAmenity.Text = dt.Rows[0]["Amenities"].ToString();
            }
            txtDescription.Text = dt.Rows[0]["Description"].ToString();

            string type = dt.Rows[0]["Type"].ToString();
            if(type == "Sell"){
                rdbSell.Checked = true;
            }
            else if (type == "Rent")
            {
                rdbRent.Checked = true;
            }else if(type == "Buy"){
                rdbBuy.Checked = true;
            }
        }

        private void GetCities(City cityObj) {
            DataTable dt = new DataTable();
            dt = citiesDalObj.GetCitiesByRegionId(cityObj);
            ddlCity.DataSource = dt;
            ddlCity.DataValueField = "CityId";
            ddlCity.DataTextField = "CityName";
            ddlCity.DataBind();
        }

        protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            Region regionObj = new Region();
            regionObj.RegionId = Convert.ToInt16(ddlRegion.SelectedValue.ToString());
            dt = GetRegionById(regionObj);
            txtRegion.Text = dt.Rows[0]["RegionName"].ToString();

            City cityObj = new City();
            cityObj.RegionId = Convert.ToInt16(ddlRegion.SelectedValue);
            GetCities(cityObj);
            ddlCity.Items.Insert(0, "--Select City--");
        }

        private DataTable GetCityById(City cityObj) {
            DataTable dt = new DataTable();
            dt = citiesDalObj.GetCitiesByCityId(cityObj);
            return dt;
        }

        private void GetDistricts(District districtObj) {
            DataTable dt = new DataTable();
            dt = districtsDalObj.GetDistrictByCityId(districtObj);
            ddlDistrict.DataSource = dt;
            ddlDistrict.DataValueField = "DistrictId";
            ddlDistrict.DataTextField = "DistrictName";
            ddlDistrict.DataBind();
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            City cityObj = new City();
            cityObj.CityId = Convert.ToInt16(ddlCity.SelectedValue);
            dt = GetCityById(cityObj);
            txtCity.Text = dt.Rows[0]["CityName"].ToString();

            District districtObj = new District();
            districtObj.CityId = Convert.ToInt16(ddlCity.SelectedValue);
            GetDistricts(districtObj);
            ddlDistrict.Items.Insert(0, "--Select District--");
        }

        private DataTable GetDistictById(District districtObj)
        {
            DataTable dt = new DataTable();
            dt = districtsDalObj.GetDistrictById(districtObj);
            return dt;
        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            District districtObj = new District();
            districtObj.DistrictId = Convert.ToInt16(ddlDistrict.SelectedValue);
            dt = GetDistictById(districtObj);
            txtDistrict.Text = dt.Rows[0]["DistrictName"].ToString();
        }

        private DataTable GetCategoryById(Category categoryObj)
        {
            DataTable dt = new DataTable();
            dt = cateDalObj.GetCategoryById(categoryObj);
            return dt;
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            Category categoryObj = new Category();
            categoryObj.CategoryId = Convert.ToInt16(ddlCategory.SelectedValue);
            dt = GetCategoryById(categoryObj);
            txtCategory.Text = dt.Rows[0]["CategoryName"].ToString();
            if(ddlCategory.SelectedItem.Text =="Land"){
                HideDivForLand();
            }
            else
            {
                ShowDivForHouseAndVillage();
            }
        }

        private void HideDivForLand()
        {
            floor.Visible = false;
            BathAndBedroom.Visible = false;
            Amenity.Visible = false;
        }

        private void ShowDivForHouseAndVillage()
        {
            floor.Visible = true;
            BathAndBedroom.Visible = true;
            Amenity.Visible = true;
        }

        private DataTable GetPackagesById(Package packObj) {
            DataTable dt = new DataTable();
            dt = parkDalObj.GetPackageById(packObj);
            return dt;
        }

        protected void ddlPackages_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            Package packageObj = new Package();
            packageObj.PackageId = Convert.ToInt16(ddlPackages.SelectedValue);
            dt = GetPackagesById(packageObj);
            txtPackages.Text = dt.Rows[0]["PackageName"].ToString();
        }
        #endregion

        #region Upload
        protected bool uploadAImage()
        {
            lblMess.Text = "";
            if (SingleUpload.HasFile)
            {
                bool check = false;
                string[] end = { ".gif", ".jpeg", ".png", ".bmp", ".wmf", ".jpg", ".GIF", ".JPEG", ".PNG", ".BMP", ".WMF", "JPG" };
                foreach (string a in end)
                {
                    if (SingleUpload.FileName.EndsWith(a))
                    {
                        check = true;
                    }
                }
                if (check)
                {
                    SingleUpload.SaveAs(Server.MapPath("..\\Single Upload") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + SingleUpload.FileName);
                    return true;
                }
                else
                {
                    lblMess.Text = "The upload file is not the image file. Please upload a image file";
                    return false;
                }
            }
            else
            {
                lblMess.Text = "Please upload the image of the advertisement.";
                return false;
            }
        }

        protected void UploadMultiFile()
        {

            if (MultiUpload.HasFile)
            {
                string count = "";
                int iUploadedCnt = 0;
                int iFailedCnt = 0;
                HttpFileCollection hfc = Request.Files;
                lblFileList.Text = "You select <b>" + hfc.Count + "</b> file(s)";

                int numImage = GetNumberImageAllowToPost(txtPackages.Text);

                if (hfc.Count <= numImage)    // NUMBER FILES RESTRICTION.
                {
                    strUrl = new string[hfc.Count];
                    for (int i = 0; i <= hfc.Count - 1; i++)
                    {
                        HttpPostedFile hpf = hfc[i];
                        if (hpf.ContentLength > 0)
                        {
                            if (!File.Exists(Server.MapPath("Images Upload\\") + Path.GetFileName(hpf.FileName)))
                            {
                                DirectoryInfo objDir = new DirectoryInfo(Server.MapPath("Images Upload\\"));
                                string sFileName = Path.GetFileName(hpf.FileName);
                                string sFileExt = Path.GetExtension(hpf.FileName);

                                // CHECK FOR DUPLICATE FILES.
                                FileInfo[] objFI = objDir.GetFiles(sFileName.Replace(sFileExt, "") + ".*");

                                if (objFI.Length > 0)
                                {
                                    // CHECK IF FILE WITH THE SAME NAME EXISTS (IGNORING THE EXTENTIONS).
                                    foreach (FileInfo file in objFI)
                                    {
                                        string sFileName1 = objFI[0].Name;
                                        string sFileExt1 = Path.GetExtension(objFI[0].Name);

                                        if (sFileName1.Replace(sFileExt1, "") == sFileName.Replace(sFileExt, ""))
                                        {
                                            iFailedCnt += 1;        // NOT ALLOWING DUPLICATE.
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    // SAVE THE FILE IN A FOLDER.
                                    hpf.SaveAs(Server.MapPath("Images Upload\\") + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetFileName(hpf.FileName));
                                    iUploadedCnt += 1;
                                    strUrl[i] = "Images Upload" + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetFileName(hpf.FileName);
                                    //adsImageObj.ImageUrl = adsImageObj.ImageUrl + "#" + "Images Upload" + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetFileName(hpf.FileName);
                                    string FileToDelete = Server.MapPath("Images Upload\\") + DateTime.Now.ToString("yyyyMMddHHmmss") + SingleUpload.FileName;
                                    count = "Images Upload" + "\\" + SingleUpload.FileName;
                                    File.Delete(FileToDelete);
                                }
                            }
                        }
                    }

                    //adsImageObj.ImageUrl = adsImageObj.ImageUrl.Remove(0, count.Replace(" ", "a").Length + 15);
                    lblUploadStatus.Text = "<b>" + iUploadedCnt + "</b> file(s) Uploaded.";
                    lblFailedStatus.Text = "<b>" + iFailedCnt + "</b> duplicate file(s) could not be uploaded.";
                }
                else lblUploadStatus.Text = "Max. 10 files allowed.";
            }
            else lblUploadStatus.Text = "No files selected.";
        }
        #endregion

        protected int GetCurrentSession()
        {

            return Convert.ToInt16(Session["adminId"]);
        }

        private void UpdateAdvertise() {
            DataTable dt = new DataTable();
            bool chk = uploadAImage();
            if (chk)
            {
                UploadMultiFile();
            }
            else
            {
                return;
            }
            dt = GetCategoryIdFromCategoryName();
            advObj.CategoryId = int.Parse(dt.Rows[0]["CategoryId"].ToString());
            advObj.UserId = GetCurrentSession();
            dt = GetDistrictIdByDistrictName();
            advObj.DistrictId = int.Parse(dt.Rows[0]["DistrictId"].ToString());
            dt = GetPackageIdByPackageName();
            advObj.ParkageId = int.Parse(dt.Rows[0]["PackageId"].ToString());
            advObj.Title = txtTitle.Text;
            if (rdbBuy.Checked)
            {
                advObj.Type = "Buy";
            }
            else if (rdbRent.Checked)
            {
                advObj.Type = "Rent";
            }
            else
            {
                advObj.Type = "Sell";
            }
            //advObj.ImageUrl = Server.MapPath("~\\Single Upload") + "\\" + SingleUpload.FileName;  //"..\\Images Upload" + "\\" + SingleUpload.FileName
            advObj.ImageUrl = "Single Upload" + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + SingleUpload.FileName;
            advObj.Price = txtPrice.Text;
            advObj.PostedDate = GetCurrentDate().ToString("MM-dd-yyyy");

            DateTime dtTime = Convert.ToDateTime(GetCurrentDate().ToString()); // DateTime.ParseExact(GetCurrentDate().ToString(), "MM-dd-yyyy", provider);
            advObj.ExpDate = (dtTime.Month + "-" + dtTime.Day + "-" + (dtTime.Year + GetDurationOfPackage())).ToString();
            advObj.LandArea = float.Parse(txtArea.Text);
            if (txtCategory.Text != "Land")
            {
                advObj.Floor = int.Parse(txtFloor.Text);
                advObj.Bedroom = int.Parse(txtBedRoom.Text);
                advObj.Bathroom = int.Parse(txtBathRoom.Text);
                advObj.Amenities = txtAmenity.Text;
                advObj.BuiltYear = int.Parse(txtBuildYear.Text);
            }
            else
            {
                advObj.Floor = 0;
                advObj.Bedroom = 0;
                advObj.Bathroom = 1000;
                advObj.Amenities = "";
                advObj.BuiltYear = 0;
            }
            advObj.Address = txtAddress.Text;
            advObj.Description = txtDescription.Text;
            advObj.IsFeatured = false;
            advObj.IsActive = true;

            DelOldImagesAtAdvertiseTable();
            DelOldImagesAtAdsImageTable();
            int result = advDalObj.UpdateAdvertisements(advObj);

            if (result > 0)
            {
                adsImageObj.AdvId = advObj.AdvId;
                
                    UpdateMultiImages();
            }
            else
            {
                Utilities.Show("Can not update your advertisements!");
            }

        }

        private void UpdateMultiImages() {
            if(rowId.Length == 0){
                return;
            }
            if (rowId.Length == (strUrl.Length - 1))
            {
                for (int i = 1; i < strUrl.Length; i++)
                {
                    adsImageObj.Id = rowId[i-1];
                    adsImageObj.ImageUrl = strUrl[i].ToString();
                    adsDal.UpdateAdsImage(adsImageObj);
                }
            }
            else if (rowId.Length > (strUrl.Length - 1))
            {
                int n = rowId.Length - (strUrl.Length - 1);
                for (int j = 0; j < n;j++ )
                {
                    adsImageObj.Id = rowId[j];
                    adsDal.DeleteAdsImagesById(adsImageObj);
                }
                for (int i = 1; i < strUrl.Length; i++)
                {
                    adsImageObj.Id = rowId[i+n-1];
                    adsImageObj.ImageUrl = strUrl[i].ToString();
                    adsDal.UpdateAdsImage(adsImageObj);
                }
            }
            else {
                Utilities.Show("You choose more than "+rowId.Length+" images");
                return;
            }
            
        }

        private DataTable GetCategoryIdFromCategoryName() {
            DataTable dt = new DataTable();
            Category cateObj = new Category();
            cateObj.CategoryName = txtCategory.Text;
            dt = cateDalObj.GetCateByCategoryName(cateObj);
            return dt;
        }

        private DataTable GetDistrictIdByDistrictName() {
            DataTable dt = new DataTable();
            District districtObj = new District();
            districtObj.DistrictName = txtDistrict.Text;
            dt = districtsDalObj.GetDistrictsByDistrictName(districtObj);
            return dt;
        }

        private DataTable GetPackageIdByPackageName()
        {
            DataTable dt = new DataTable();
            Package packObj = new Package();
            packObj.PackageName = txtPackages.Text;
            dt = parkDalObj.GetPackageByName(packObj);
            return dt;
        }

        private DateTime GetCurrentDate()
        {
            return DateTime.Now;
        }

        private int GetDurationOfPackage()
        {
            DataTable dt = new DataTable();
            dt = GetPackageIdByPackageName();
            Package pObj = new Package();
            pObj.PackageId = int.Parse(dt.Rows[0]["PackageId"].ToString());
            dt = parkDalObj.GetPackageById(pObj);

            return int.Parse(dt.Rows[0]["Duration"].ToString());
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateAdvertise();
            string message = "Congratulation! You updated successfully!";
            string url = "MyAdvertisement.aspx";
            Utilities.ShowAndRedirect(message, url);
        }

        protected void DelOldImagesAtAdvertiseTable() {
            DataTable dt = new DataTable();
            dt = advDalObj.GetImageUrlById(advObj);
            string imageUrlAdv = dt.Rows[0]["ImageUrl"].ToString().Remove(0,14);
            string FileToDelete = Server.MapPath(".").Replace("AdminCP","")+"Single Upload\\"+imageUrlAdv;
            FileInfo theFile = new FileInfo(Server.MapPath(".").Replace("AdminCP", "") + "Single Upload\\" + imageUrlAdv);
            if (theFile.Exists)
            {
                File.Delete(FileToDelete);
            }
            //else
            //{
            //    Utilities.Show("Your image that you updated not exist!");
            //}
        }

        protected void DelOldImagesAtAdsImageTable() {
            DataTable dt = new DataTable();
            AdsImages adsObj = new AdsImages();
            adsObj.AdvId = advObj.AdvId;
            dt = adsDal.GetImageUrlById(adsObj);
            rowId = new int[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++ )
            {
                rowId[i] = int.Parse(dt.Rows[i]["Id"].ToString());
                string imageUrlAdv = dt.Rows[i]["ImageUrl"].ToString().Remove(0, 14);
                string FileToDelete = Server.MapPath(".") + "\\"+"Images Upload\\" + imageUrlAdv;
                FileInfo theFile = new FileInfo(Server.MapPath(".") +"\\"+ "Images Upload\\" + imageUrlAdv);
                if (theFile.Exists)
                {
                    File.Delete(FileToDelete);
                }
                //else {
                //    Utilities.Show("Your image that you updated not exist!");
                //}
            }
        }

        private int GetNumberImageAllowToPost(string packName) {
            DataTable dt = new DataTable();
            Package packObj = new Package();
            packObj.PackageName = packName;
            dt = parkDalObj.GetPackageByName(packObj);
            return int.Parse(dt.Rows[0]["NumOfImg"].ToString());
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AdminCP/MyAdvertisement.aspx");
        }
    }
}